package com.example.ncov_management_system.controller;

import com.example.ncov_management_system.entity.Record;
import com.example.ncov_management_system.entity.Student;
import com.example.ncov_management_system.service.MessageService;
import com.example.ncov_management_system.service.RecordService;
import com.example.ncov_management_system.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/record")
@Slf4j
public class RecordController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private MessageService messageService;

    @GetMapping("/to_add")
    public String toAdd(Model model, HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        model.addAttribute("isReported", recordService.isReported(student.getId()));
        return "record_add";
    }

    @PostMapping("/add")
    public String add(Record record, HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        Student student_db = studentService.findById(student.getId());
        record.setCreateTime(LocalDateTime.now());
        record.setStudent(student_db);
        recordService.add(record);
        return "redirect:/student/index";
    }

    @GetMapping("/list/{clbum_id}")
    public String list(@PathVariable long clbum_id, @RequestParam(name = "pn", defaultValue = "0") int pn, String date, Model model) {
        Page<Record> page = null;
        if (StringUtils.isEmpty(date)) {
            page = recordService.findByClbumId(clbum_id, LocalDate.now(), pn);
        } else {
            page = recordService.findByClbumId(clbum_id, LocalDate.parse(date), pn);
        }
        model.addAttribute("page", page);
        return "record_list";
    }

    @GetMapping("/un_reported/{clbum_id}")
    public String unReported(@PathVariable long clbum_id, @RequestParam(name = "pn", defaultValue = "0") int pn, Model model) {
        ArrayList<Student> result = new ArrayList<>();
        List<Student> studentList = studentService.findByClbumId(clbum_id);
        log.info("studentList:{}", studentList);
        List<Student> reprotedList = recordService.findReportedStudentByClbumId(clbum_id);
        log.info("reprotedList:{}", reprotedList);
        for (Student student : studentList) {
            if (!reprotedList.contains(student)) {
                student.setRemind(messageService.isRemind(student.getId()));
                result.add(student);
            }
        }
        log.info("unreported:{}", result);
        PageRequest pageRequest = PageRequest.of(pn, 10);
        int fromIndex = pageRequest.getPageNumber()*pageRequest.getPageSize();
        int toIndex = (pageRequest.getPageNumber()+1)*pageRequest.getPageSize();
        PageImpl<Student> page = new PageImpl<>(result.subList(fromIndex,Math.min(toIndex,result.size())),pageRequest , result.size());
        model.addAttribute("page",page);
        return "record_unreported";
    }

    @GetMapping("/person_record")
    public String personRecord(@RequestParam(name = "pn",defaultValue = "0")int pn, HttpSession session,Model model) {
        Student student = (Student) session.getAttribute("student");
        Student student_db = studentService.findById(student.getId());
        List<Record> records = student_db.getRecords();
        log.info("records:{}",records);
        PageRequest pageRequest = PageRequest.of(pn, 10);
        int fromIndex = pageRequest.getPageNumber()*pageRequest.getPageSize();
        int toIndex = (pageRequest.getPageNumber()+1)*pageRequest.getPageSize();
        PageImpl<Record> page = new PageImpl<>(records.subList(fromIndex,Math.min(toIndex,records.size())),pageRequest , records.size());
        model.addAttribute("page", page);
        return "student_record_list";
    }
}
